/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年6月17日
 * @version 0.1
 */
package com.suredy.base.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.base.model.Permission;
import com.suredy.base.model.Role2Permission;
import com.suredy.core.service.BaseSrvWithEntity;


/**
 * @author ZhangMaoren
 *
 */
@Service("Role2PermissionSrv")
public class Role2PermissionSrv extends BaseSrvWithEntity<Role2Permission>{
	@Override
	public DetachedCriteria getDc(Role2Permission t) {
		DetachedCriteria dc = super.getDc(t);

		if (t == null) {
			return dc;
		}
		
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		
		if (!StringUtils.isBlank(t.getRoleId())) {
			dc.add(Restrictions.eq("roleId", t.getRoleId()));
		}
		
		if (t.getRolePermission() != null) {
			dc.add(Restrictions.eq("rolePermission", t.getRolePermission()));
		}

		return dc;
	}
	
	public List<Role2Permission> getAll() {
		return this.readByEntity(null);
	}

	public Role2Permission get(String id) {
		return this.get(id);
	}
	
	public List<Role2Permission> getByRole(String roleId) {
		List<Role2Permission> ret = new ArrayList<Role2Permission>(); 
		if (StringUtils.isBlank(roleId)) {
			return ret;
		}
		Role2Permission search = new Role2Permission();
		search.setRoleId(roleId);
		ret = this.readByEntity(search);
		return ret;
	}
	
	public List<Role2Permission> getByPermission(String permissionId) {
		List<Role2Permission> ret = new ArrayList<Role2Permission>(); 
		if (StringUtils.isBlank(permissionId)) {
			return ret;
		}
		Role2Permission search = new Role2Permission();
		Permission permission = new Permission();
		permission.setId(permissionId);
		search.setRolePermission(permission);
		ret = this.readByEntity(search);
		return ret;
	}

}
